//
//  KeleTabBar.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/10.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "KeleTabBar.h"

@implementation KeleTabBar

- (NSMutableDictionary *)dic{
    if (!_dic) {
        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}


- (instancetype) initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        UIView *backColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KLScreenW, 48)];
        backColorView.backgroundColor = [UIColor whiteColor];
        [self insertSubview:backColorView atIndex:0];
        
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabbar_icon_shangchuan"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabbar_icon_shangchuan"] forState:UIControlStateHighlighted];
        [[publishButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) {
            if (self.delegateSignal) {
                [self.delegateSignal sendNext:nil];
            }
        }];
        [self addSubview:publishButton];
        self.uploadButton = publishButton;
    }
    return self;
}
// layout item


// layout item
- (void)layoutSubviews{
    
    [super layoutSubviews];
    CGFloat width = self.width;
    CGFloat height = self.height;
    
    // 设置发布按钮的frame
    self.uploadButton.bounds = CGRectMake(0, 0, self.uploadButton.currentBackgroundImage.size.width, self.uploadButton.currentBackgroundImage.size.height);
    self.uploadButton.center = CGPointMake(width * 0.5, height * 0.5);
    // 设置其他的UiTabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW =width / 5;
    CGFloat buttonH =height;
    NSInteger index = 0;
    
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1) : index);
        button.frame = CGRectMake(buttonX,buttonY,buttonW,buttonH);
        // 增加索引
        index ++;
        
        
    }
}

//重写hitTest方法，去监听发布按钮的点击，目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    //这一个判断是关键，不判断的话push到其他页面，点击发布按钮的位置也是会有反应的，这样就不好了
    //self.isHidden == NO 说明当前页面是有tabbar的，那么肯定是在导航控制器的根控制器页面
    //在导航控制器根控制器页面，那么我们就需要判断手指点击的位置是否在发布按钮身上
    //是的话让发布按钮自己处理点击事件，不是的话让系统去处理点击事件就可以了
    if (self.isHidden == NO) {
        
        //将当前tabbar的触摸点转换坐标系，转换到发布按钮的身上，生成一个新的点
        CGPoint newP = [self convertPoint:point toView:self.uploadButton];
        
        //判断如果这个新的点是在发布按钮身上，那么处理点击事件最合适的view就是发布按钮
        if ( [self.uploadButton pointInside:newP withEvent:event]) {
            return self.uploadButton;
        }else{//如果点不在发布按钮身上，直接让系统处理就可以了
            return [super hitTest:point withEvent:event];
        }
    }
    else {//tabbar隐藏了，那么说明已经push到其他的页面了，这个时候还是让系统去判断最合适的view处理就好了
        return [super hitTest:point withEvent:event];
    }
}





@end
